package com.smartcruise.util.network.baseApi;

import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by FengChaoQun
 * on 2017/7/12 0012
 * 构造Map<String, RequestBody>的工具类 用于发送Multipart网络请求
 * <p>
 * modified by FengChaoQun on 2017/7/26 0026 15:53
 * description:上传图片的时候 有时候会对图片进行压缩 这是一个耗时的过程 统一放在{@link #build()}中 方便异步处理
 * 建议将该类配合{@link com.smartcruise.util.network.baseApi.NetApiObservableFactory#normalMultipartObservable(String, MultipartBodyBuilder)}
 * 构造一个Observable  将图片压缩和网络请求放在一个异步线程中进行有效避免主线程阻塞
 */

public class MultipartBodyBuilder {

    private List<CompressImageInfo> compressImageInfos = new ArrayList<>();

    private Map<String, RequestBody> requestBodyMap = new LinkedHashMap<>();//使用LinkedHashMap，可以保存图片上传的顺序

    public MultipartBodyBuilder addText(String key, String value) {
        if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
            return this;
        }
        requestBodyMap.put(key, RequestBody.create(MediaType.parse("text/plain"), value));
        return this;
    }

    public MultipartBodyBuilder addImage(String key, String imagePath) {
        if (TextUtils.isEmpty(imagePath)) {
            return this;
        }
        return addImage(key, new File(imagePath));
    }

    private MultipartBodyBuilder addImage(String key, File file) {
        if (file != null && file.exists()) {
            requestBodyMap.put(key + "\"; filename=\"" + file.getName() + "", RequestBody.create(MediaType.parse("image/*"), file));
        }
        return this;
    }

    public MultipartBodyBuilder addImages(String key, List<String> paths, boolean compress) {
        if (paths == null || paths.size() == 0) {
            return this;
        }
        for (String path : paths) {
            if (!TextUtils.isEmpty(path)) {
                if (compress) {
                    compressImageInfos.add(new CompressImageInfo(key, path));
                } else {
                    addImage(key, path);
                }
            }
        }
        return this;
    }

    public MultipartBodyBuilder addFile(String key, File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        requestBodyMap.put(key + "\"; filename=\"" + file.getName() + "", requestFile);
        return this;
    }

    public Map<String, RequestBody> build() {
//        long time = TimeUtil.currentTimeMillis();
//        for (CompressImageInfo compressImageInfo : compressImageInfos) {
//            addImage(compressImageInfo.key, SendImageHelper.getLittleImage(compressImageInfo.path));
//        }
//        Log.d("MultipartBodyBuilder", "compress image use time:" + (TimeUtil.currentTimeMillis() - time));
        return this.requestBodyMap;
    }

    private class CompressImageInfo {
        private String path;
        private String key;

        CompressImageInfo(String key, String path) {
            this.path = path;
            this.key = key;
        }
    }
}
